<template>
  <div ref="chartRef" :style="{ height, width }"></div>
</template>
<script lang="ts" setup>
import {reactive, Ref, ref, watch} from 'vue';
import {useECharts} from '/@/hooks/web/useECharts';
import {basicProps} from './props';
import {queryChartStatistics} from '../api';

const props = defineProps({
    ...basicProps,
    activeKey: {
      type: Number,
      default: 1,
    },
    dateType: {
      type: Number,
      default: 1,
    },
  });
  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);
  const option = reactive({
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        lineStyle: {
          width: 1,
          color: '#019680',
        },
      },
    },
    grid: { left: '1%', right: '1%', top: '2%', bottom: 0, containLabel: true },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: [],
      splitLine: {
        show: true,
        lineStyle: {
          width: 1,
          type: 'solid',
          color: 'rgba(226,226,226,0.5)',
        },
      },
      axisTick: {
        show: false,
      },
    },
    yAxis: [
      {
        type: 'value',
        splitNumber: 4,
        axisTick: {
          show: false,
        },
        splitArea: {
          show: true,
          areaStyle: {
            color: ['rgba(255,255,255,0.2)', 'rgba(226,226,226,0.2)'],
          },
        },
      },
    ],
    series: [
      {
        name: '发放金额',
        smooth: true,
        data: [],
        type: 'line',
        areaStyle: {},
        itemStyle: {
          color: '#5ab1ef',
        },
      },
      {
        name: '服务费',
        smooth: true,
        data: [],
        type: 'line',
        areaStyle: {},
        itemStyle: {
          color: '#019680',
        },
      },
    ],
  });

  watch(
    () => [props.activeKey, props.dateType],
    () => {
      initChartStatistics(props.activeKey, props.dateType);
    },
    {immediate: true}
  );

  /**
   * 获取发放趋势统计
   */
  function initChartStatistics(type, dateType) {
    queryChartStatistics({type: type, dateType: dateType}).then((res) => {
      if (res.success) {
        let xAxisData = res.result.map((item) => {
          return item.name;
        });
        let seriesData = res.result.map((item) => {
          return item.value;
        });
        let seriesData1 = res.result.map((item) => {
          return item.value1;
        });
        option.xAxis.data = xAxisData;
        option.series[0].data = seriesData;
        option.series[1].data = seriesData1;
        setOptions(option);
      }
    });
  }
</script>
